package com.xish.spring.exception;

import com.xish.util.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class Common200ExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(Common200ExceptionHandler.class);

    public void handle(HttpServletResponse response, Common200Exception exception) {
        response.setStatus(exception.getHttpStatusCode());
        response.setContentType(MediaType.APPLICATION_JSON);

        try {
            response.getWriter().print(exception.getMessage());
        } catch (IOException e) {
            LOG.error("IOException", e);
        }
    }
}
